﻿@inherits NITASA.Views.AutofacEnabledViewPage<List<Tuple<User, string>>>
@using NITASA.Data
@using GridMvc.Html
@using NITASA.Services.Security
@{
    ViewBag.Title = "Users";
}
        <div class="main-content">
            <div class="container-fluid padded">
                @if (TempData["SuccessMessage"] != null)
                {
                    <div class="padded">
                        <div class="alert alert-success">
                            <button class="close" data-dismiss="alert" type="button">×</button>
                            @TempData["SuccessMessage"]
                        </div>
                    </div>
                }
                @if (TempData["ErrorMessage"] != null)
                {
                    <div class="padded">
                        <div class="alert alert-error">
                            <button class="close" data-dismiss="alert" type="button">×</button>
                            @TempData["ErrorMessage"]
                        </div>
                    </div>
                }
                <div class="row-fluid">
                    <div class="span12">
                        <div class="box">
                            <div class="box-header">
                                <span class="title">Browse Users</span>
                                @*@if (aclService.HasRight(Rights.CreateNewUsers))*@
                                @if (aclService.HasRight(Rights.CreateNewUsers))
                                {   
                                    <ul class="box-toolbar">
                                        <li><a href="@Url.Action("Add")" title="Add New User"><i class="icon-plus-sign" style="margin-top: 10px"></i></a></li>
                                    </ul>
                                }
                            </div>
                            <div class="box-content">
                                @if (Model.Count() > 0)
                                {
                                    @Html.Grid(Model).Columns(columns =>
                                            {
                                                columns.Add(x => x.Item1.FullName).Titled("Name").Sortable(true).Filterable(true);
                                                columns.Add(x => x.Item1.Email).Titled("Email").Sortable(true).Filterable(true);
                                                columns.Add(x => x.Item2).Titled("Roles").Sortable(true).Filterable(true);
                                                columns.Add(x => x.Item1.IsActive).Titled("Status").RenderValueAs(x => x.Item1.IsActive ? "Active" : "In Active").Sortable(true).Filterable(true);
                                                columns.Add().SetWidth(110).Titled("Change Status").RenderValueAs(x => RenderStatusButton(x, aclService)).Sanitized(false).Encoded(false);
                                                columns.Add().SetWidth(110).Titled("").RenderValueAs(x => RenderActions(x,aclService)).Sanitized(false).Encoded(false);
                                            }).WithPaging(10, 5).WithMultipleFilters()
                                }
                                else
                                {
                                    <div class="padded">No Users Found.</div>
                                }
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div style="display:none">
            @using (Html.BeginForm("changeStatus", "User", FormMethod.Get, new { id = "frmCS" }))
            {
                <input type="hidden" name="isActive" id="isActive" />
                <input type="hidden" name="GUID" id="GUID" />
            }
        </div>

        <script>
            function changestatus(obj, GUID) {
                $("#isActive").val(obj.checked);
                $("#GUID").val(GUID);
                $("#frmCS").submit();
            };
        </script>

@helper RenderStatusButton(Tuple<User, string> usr,IAclService aclService)
{
    if (usr.Item1.IsSuperAdmin == false)
    {
        if (NITASA.Areas.Admin.Helper.Functions.CurrentUserID() == usr.Item1.ID)
        {
            //if (aclService.HasRight(Rights.EditUserSelf))
            if (aclService.HasRight(Rights.EditUserSelf))
            {
                <input class="iButton-icons" type="checkbox" onchange="changestatus(this,'@usr.Item1.GUID');" @(usr.Item1.IsActive == true ? "checked='checked'" : "") />
            }
        }
        else
        {
            //if (aclService.HasRight(Rights.EditOtherUsers))
            if (aclService.HasRight(Rights.EditOtherUsers))
            {
                <input class="iButton-icons" type="checkbox" onchange="changestatus(this,'@usr.Item1.GUID');" @(usr.Item1.IsActive == true ? "checked='checked'" : "") />
            }
        }
    }
}
@helper RenderActions(Tuple<User, string> usr,IAclService aclService)
{
    if (usr.Item1.IsSuperAdmin == false)
    {
        if (NITASA.Areas.Admin.Helper.Functions.CurrentUserID() == usr.Item1.ID)
        {
            //if (aclService.HasRight(Rights.EditUserSelf))
            if (aclService.HasRight(Rights.EditUserSelf))
            {
                @Html.ActionLink("Edit", "Edit", new { GUID = usr.Item1.GUID }, new { @class = "btn btn-small btn-blue" })
            }
        }
        else
        {
            //if (aclService.HasRight(Rights.EditOtherUsers))
            if (aclService.HasRight(Rights.EditOtherUsers))
            {
                @Html.ActionLink("Edit", "Edit", new { GUID = usr.Item1.GUID }, new { @class = "btn btn-small btn-blue" })
            }
        }
        if (NITASA.Areas.Admin.Helper.Functions.CurrentUserID() == usr.Item1.ID)
        {
            //if (aclService.HasRight(Rights.DeleteUserSelf))
            if (aclService.HasRight(Rights.DeleteUserSelf))
            {
                @Html.ActionLink("Delete", "Delete", new { GUID = usr.Item1.GUID }, new { @class = "btn btn-small btn-red", onclick = "return confirm('Are you sure to delete this User ?');" })
            }
        }
        else
        {
            //if (aclService.HasRight(Rights.DeleteOtherUsers))
            if (aclService.HasRight(Rights.DeleteOtherUsers))
            {
                @Html.ActionLink("Delete", "Delete", new { GUID = usr.Item1.GUID }, new { @class = "btn btn-small btn-red", onclick = "return confirm('Are you sure to delete this User ?');" })
            }
        }
    }
}
